// pages/addMembers/addMembers.js
var app = getApp()
var QQMapWX = require('../../qqmap-wx-jssdk.min');
var qqmapsdk



Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 姓名
    name: "",
    // 城市
    region: [],
    // 电话
    phone: "",
    // 头像
    headerImage: "",
    // 业务员的id
    salesmanId: '',
    // 成功遮罩层
    issuccessMask: false,
    // 销售地区
    salesArea: '',
    // 代理商id
    agentId: '',
    // 客编
    identifier: '',
    // 门店名称
    shopName: '',
    // 店长id
    shopownerId: '',
    beginWorkDate: '',
    // 工作时间
    service: '',

    // 岩板砖家id
    superviseId: '',
    // 店长的门店地址
    shopAddress: '',

    // 添加的是啥
    addtype: 1,
    // 大区区域
    region: '',
    // 销区列表
    areaList: '',
    regionindex: 0,
    areaListIndex: 0,
    // 设计师id
    designerId: '',
    // 家居顾问
    adviserId: ''
  },
  // 大区区域
  bindregionChang(e) {
    this.setData({
      regionindex: e.detail.value,
      region: this.data.saleArea[e.detail.value].region
    })
  },
  // 销区搜索
  bindareaListChange(e) {
    this.setData({
      areaListIndex: e.detail.value,
      areaList: this.data.saleArea[this.data.regionindex].areaList[e.detail.value].name
    })
  },

  // 从业时间
  bindRegionChangeservice(e) {
    this.setData({
      service: e.detail.value
    })
  },
  fakeCallback() {},
  // 28.添加业务员
  saveSalesman() {
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    let t = this
    // console.log(t.data.phone,t.data.name)
    // return
    wx.request({
      url: app.globalData.Interfaceurl + 'saveSalesman',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        token: t.data.token,
        salesmanId: t.data.salesmanId,
        name: t.data.name,
        mobile: t.data.phone,
        region: t.data.region,
        saleArea: t.data.areaList
      },
      success: res => {
        wx.hideLoading()
        console.log(res)
        if (res.data.result == 0) {
          t.setData({
            issuccessMask: true
          })

        } else {
          wx.showToast({
            title: res.data.message,
            icon: "none"
          })
          t.setData({
            token: app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },
  // 添加代理商
  saveAgent() {
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    let t = this
    // console.log(t.data.phone,t.data.name)
    // return
    wx.request({
      url: app.globalData.Interfaceurl + 'saveAgent',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        token: t.data.token,
        agentId: t.data.agentId,
        name: t.data.name,
        mobile: t.data.phone,
        identifier: t.data.identifier,
      },
      success: res => {
        wx.hideLoading()
        console.log(res)
        if (res.data.result == 0) {
          t.setData({
            issuccessMask: true
          })

        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token: app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })

  },
  // 添加店长
  saveShopowner() {
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    let t = this
    // console.log(t.data.phone,t.data.name)
    // return
    wx.request({
      url: app.globalData.Interfaceurl + 'saveShopowner',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        token: t.data.token,
        shopownerId: t.data.shopownerId,
        name: t.data.name,
        mobile: t.data.phone,
        beginWorkDate: t.data.service,
        shopName: t.data.shopName,
        province: t.data.region[0],
        city: t.data.region[1],
        area: t.data.region[2],
        shopAddress: t.data.shopAddress,
      },
      success: res => {
        wx.hideLoading()
        console.log(res)
        if (res.data.result == 0) {
          t.setData({
            issuccessMask: true
          })

        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token: app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })

  },
  // 添加岩板砖家
  saveSupervise() {
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    let t = this
    // console.log(t.data.phone,t.data.name)
    // return
    wx.request({
      url: app.globalData.Interfaceurl + 'saveSupervise',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        token: t.data.token,
        superviseId: t.data.superviseId,
        name: t.data.name,
        mobile: t.data.phone,
        shopNo: t.data.shopNo,
        shopName: t.data.shopName,
        beginWorkDate: t.data.service,
        province: t.data.region[0],
        city: t.data.region[1],
        area: t.data.region[2],
      },
      success: res => {
        wx.hideLoading()
        console.log(res)
        if (res.data.result == 0) {
          t.setData({
            issuccessMask: true
          })

        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token: app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
      }
    })

  },
  // 添加家居顾问
  saveAdviser() {
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    let t = this
    // console.log(t.data.phone,t.data.name)
    // return
    wx.request({
      url: app.globalData.Interfaceurl + 'saveAdviser',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        token: t.data.token,
        adviserId: t.data.adviserId,
        name: t.data.name,
        mobile: t.data.phone,
        beginWorkDate: t.data.service,
      },
      success: res => {
        wx.hideLoading()
        console.log(res)
        if (res.data.result == 0) {
          t.setData({
            issuccessMask: true
          })

        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token: app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })

  },


  // 83.销区列表
  getSaleArea() {
    wx.showLoading({
      title: '加载中',
    })
    let t = this
    // console.log(t.data.phone,t.data.name)
    // return
    wx.request({
      url: app.globalData.Interfaceurl + 'getSaleArea',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
      },
      success: res => {
        wx.hideLoading()
        this.setData({
          saleArea: res.data.saleArea
        })
        console.log(res)
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })

  },
  // 隐藏遮罩层
  HidesuccessMask() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\

    wx.setStorageSync('istab', true)

    this.setData({
      issuccessMask: false
    })

    wx.navigateBack({
      delta: 1,
    })
  },

  bindRegionChange(e) {
    this.setData({
      region: e.detail.value
    })
  },

  bindPlone(e) {
    console.log(e)
    let phone = e.detail.value
    let phoneCodeVerification = /^[1][0-9]{10}$/;
    if (!phoneCodeVerification.test(phone)) {
      wx.showToast({
        title: '您输入手机号码输入不正确',
        duration: 2500, //持续的时间
            icon: 'none', //图标
      })
      this.setData({
        phone: ""
      })
    } else {

    }

  },
  bindadd() {
    if (this.data.name == '') {
      wx.showToast({
        title: '请输入姓名',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }
    if (this.data.phone == '') {
      wx.showToast({
        title: '请输入手机号',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }
    if (app.globalData.identity == 3 || app.globalData.identity == 5) {
      if (this.data.service == '') {
        wx.showToast({
          title: '请输入工龄',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
    }


    if (this.data.todo == 'addsalesman') {
      if (this.data.region == '' || this.data.areaList == '') {
        wx.showToast({
          title: '销区不能为空',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }



      this.saveSalesman()
    } else if (this.data.todo == 'addagent') {
      if (this.data.Identifier == '') {
        wx.showToast({
          title: '请输入客编',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      this.saveAgent()
    } else if (this.data.todo == 'addshopowner') {
      if (this.data.shopName == '') {
        wx.showToast({
          title: '请填写门店名称',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      if (this.data.region.length == 0) {
        wx.showToast({
          title: '请选择门店位置',
           duration: 2500, //持续的时间
           duration: 2500, //持续的时间
           icon: 'none', //图标
        })
        return
      }
      if (this.data.shopAddress == '') {
        wx.showToast({
          title: '请填写详细地址',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }



      this.saveShopowner()
    } else if (this.data.todo == 'addsupervisor') {
      this.saveSupervise()
    } else if (this.data.todo == 'adddesigner') {
      this.saveDesigner()
    } else if (this.data.todo == 'addstaff') {
      this.saveAdviser()
    }
  },
  radioChange(e) {
    this.setData({
      addtype: e.detail.value
    })
    if (this.data.addtype == 1) {
      this.setData({
        todo: 'addshopowner'
      })
    } else {
      this.setData({
        todo: 'addsupervisor'
      })
    }
  },


  // 添加设计师
  saveDesigner() {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'saveDesigner',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        name: t.data.name,
        designerId: t.data.designerId,
        token: t.data.token,
        mobile: t.data.phone,
        beginWorkDate: t.data.service
      },
      success: res => {
        console.log(res)
        wx.hideLoading()
        if (res.data.result == 0) {
          wx.setStorageSync('istab', true)
          this.setData({
            issuccessMask: true
          })
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token: app.token()
          })
        }

      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获得当前时间
    let date=new Date()
    let year=date.getFullYear()
    let mouth=date.getMonth()+1
    mouth=mouth>=10?mouth:"0"+mouth
    let d=date.getDate()
    d=d>=10?d:"0"+d




    this.setData({
      identity: app.globalData.identity,
      token: app.token(),
      todo: options.todo,
      nowdate:year+"-"+mouth,
      headerImage: app.globalData.Imageurl+"/resource/userfiles/shunhui/shunhui_user_head.png",
    })
    if (app.globalData.identity == 1 || app.globalData.identity == 2) {
      this.getSaleArea()
    }
    if (options.todo == 'addsupervisor' || options.todo == 'addshopowner') {
      let t = this
      // 实例化API核心类
      qqmapsdk = new QQMapWX({
        key: 'INUBZ-UKHCJ-KS5FI-FDQMU-TXCXV-ZHFSH'
      });
      wx.getLocation({
        type: 'wgs84',
        success: (res) => {
          this.setData({
            latitude_value: res.latitude,
            longitude_value: res.longitude
          })
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: this.data.latitude_value,
              longitude: this.data.longitude_value
            },
            success: (res) => { //成功后的回调
              var city = res.result.ad_info.city;
              // console.log(res)
              if (options.detail) {

              } else {
                t.setData({
                  region: [res.result.address_component.province, res.result.address_component.city, res.result.address_component.district]
                })
              }

            }
          })
        }
      })
    }

    if (options.todo == 'addsupervisor') {
      this.setData({
        addtype: 2
      })
    } else if (options.todo == 'addshopowner') {
      this.setData({
        addtype: 1
      })
    } else if (options.todo == 'adddesigner') {
      this.setData({
        addtype: 1
      })
      
    } else if (options.todo == 'addstaff') {
      this.setData({
        addtype: 2
      })
    }



    if (options.detail) {
      let detail = JSON.parse(options.detail)
      this.setData({
        detail: detail
      })
      console.log(detail)
      if (this.data.todo == 'addsalesman') {
        this.setData({
          name: detail.name,
          phone: detail.mobile,
          salesmanId: detail.id,
          headerImage: detail.image,
          region: detail.region,
          areaList: detail.saleArea,
        })

      } else if (this.data.todo == 'addagent') {
        this.setData({
          name: detail.name,
          phone: detail.mobile,
          agentId: detail.id,
          identifier: detail.identifier,
          headerImage: detail.image,
        })
      } else if (this.data.todo == 'addshopowner') {




        this.setData({
          name: detail.name,
          phone: detail.mobile,
          shopownerId: detail.id,
          headerImage: detail.image,
          service: detail.beginWorkDate,
          shopAddress: detail.shopAddress,
          shopName: detail.shopName,
          region: [detail.province, detail.city, detail.area],

        })
      } else if (this.data.todo == 'addsupervisor') {
        this.setData({
          name: detail.name,
          phone: detail.mobile,
          shopownerId: detail.id,
          shopName: detail.shopName,
          shopNo: detail.shopNo,
          headerImage: detail.image,
          service: detail.beginWorkDate,
          region: [detail.province, detail.city, detail.area],

        })
      } else if (this.data.todo == 'adddesigner' || this.data.todo == 'addstaff') {
        this.setData({
          name: detail.name,
          phone: detail.mobile,
          service: detail.beginWorkDate,

        })
      }
      if (app.globalData.identity == 3) {
        let t = this
        // 实例化API核心类
        qqmapsdk = new QQMapWX({
          key: 'INUBZ-UKHCJ-KS5FI-FDQMU-TXCXV-ZHFSH'
        });
        wx.getLocation({
          type: 'wgs84',
          success: (res) => {
            this.setData({
              latitude_value: res.latitude,
              longitude_value: res.longitude
            })
            qqmapsdk.reverseGeocoder({
              location: {
                latitude: this.data.latitude_value,
                longitude: this.data.longitude_value
              },
              success: (res) => { //成功后的回调
                var city = res.result.ad_info.city;
                // console.log(res)
                t.setData({
                  region: [res.result.address_component.province, res.result.address_component.city, res.result.address_component.district]
                })
              }
            })
          }
        })
      }




    }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})